import { t } from 'i18next'
import { useState } from 'react'
import shallow from 'zustand/shallow'
import { getMtIdList, checkData, getSmsParam, SmsTextArea, checkMsg, SmsEntityEdit } from '../translator'
import { Alert } from '@/components/Alert'
import { fetchClearMsg, fetchSaveMsg, fetchSendMsg, fetchStopMsg } from '@/api/manager/sms'
import { useSmsStore } from '../store'
import { useMccStore } from '@/pages/manager/store'

export const useData = () => {
	const { list, receiVTerminal, rollTimeChecked, rollTime, model, speed, smsList, update, fetchSmsList } =
		useSmsStore(
			(state) => ({
				list: state.list,
				receiVTerminal: state.receiVTerminal,
				rollTimeChecked: state.rollTimeChecked,
				rollTime: state.rollTime,
				model: state.model,
				speed: state.speed,
				smsList: state.smsList,
				update: state.update,
				fetchSmsList: state.fetchSmsList,
			}),
			shallow
		)
	const [smsText, setSmsText] = useState<SmsTextArea>({
		smsTextArea: '',
		smsTextAreaModal: '',
		smsTextAreaModalEdit: '',
	})
	const updateTextArea = (param: Partial<SmsTextArea>) => {
		const text = Object.assign(smsText, param)
		setSmsText({ ...text })
	}
	const stopMessage = async () => {
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		let res
		if (receiVTerminal === 1) {
			//全部
			res = await fetchStopMsg({ meetingMoid })
		} else {
			//自定义
			if (list.length === 0) {
				Alert(t('请选择与会方'))
				return
			}
			res = await fetchStopMsg({
				meetingMoid,
				mts: getMtIdList(list),
			})
		}
		if (res) {
			Alert(t('停止短消息成功'))
		} else {
			Alert(t('停止短消息失败'))
		}
	}
	const sendMessage = async () => {
		let res
		if (checkData(smsText.smsTextArea, rollTimeChecked, rollTime, model)) {
			const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
			if (receiVTerminal === 1) {
				//全部
				res = await fetchSendMsg(
					getSmsParam(smsText.smsTextArea, model, rollTime, speed, rollTimeChecked),
					meetingMoid
				)
			} else {
				//自定义
				if (list.length === 0) {
					Alert(t('请选择与会方'))
					return
				}
				const mts = getMtIdList(list)
				res = await fetchSendMsg(
					getSmsParam(smsText.smsTextArea, model, rollTime, speed, rollTimeChecked, mts),
					meetingMoid
				)
			}
			if (res) {
				Alert(t('发送短消息成功'))
				updateTextArea({ smsTextArea: '' })
			} else {
				Alert(t('发送短消息失败'))
			}
		}
	}
	//添加常用消息
	const addCommonUseSms = async () => {
		if (checkMsg(smsText.smsTextAreaModal)) {
			const res = await fetchSaveMsg({ id: '', msg: smsText.smsTextAreaModal })
			if (res.success) {
				Alert(t('添加成功'))
				updateTextArea({ smsTextAreaModal: '' })
				fetchSmsList()
			} else {
				Alert(res.description || t('添加失败'))
			}
		}
	}
	//修改常用消息
	const updateSingleMsg = async (id: string, msg: string) => {
		if (checkMsg(msg)) {
			const res = await fetchSaveMsg({ id: id, msg: msg })
			if (res.success) {
				Alert(t('修改成功'))
				updateTextArea({ smsTextAreaModal: '' })
				fetchSmsList()
			} else {
				Alert(res.description || t('修改失败'))
			}
		}
	}
	const clearAllMsg = async () => {
		const res = await fetchClearMsg()
		if (res) {
			Alert(t('清空成功'))
			updateTextArea({ smsTextAreaModal: '' })
			fetchSmsList()
		} else {
			Alert(t('清空失败'))
		}
	}
	//点击修改
	const insertMsg = (x: SmsEntityEdit, e: any) => {
		smsList.forEach((item) => {
			item.id === x.id ? (item.isShowEdit = true) : (item.isShowEdit = false)
		})
		updateTextArea({ smsTextAreaModalEdit: x.msg })
		update({ smsList: [...smsList] })
	}
	//关闭修改短消息
	const closeEdit = () => {
		smsList.forEach((item) => {
			item.isShowEdit = false
		})
		update({ smsList: [...smsList] })
	}
	//删除单个短消息
	const deleteMsg = async (id: string, e: any) => {
		e.stopPropagation()
		const res = await fetchClearMsg(id)
		if (res) {
			Alert(t('删除成功'))
			updateTextArea({ smsTextAreaModal: '' })
			fetchSmsList()
		} else {
			Alert(t('删除失败'))
		}
	}
	return {
		smsText,
		smsList,
		updateTextArea,
		stopMessage,
		sendMessage,
		addCommonUseSms,
		updateSingleMsg,
		clearAllMsg,
		closeEdit,
		deleteMsg,
		insertMsg,
	}
}
